
/**
 * nobittermodel.h
 * 
 * NobitterModel is the model class to hold all Tweet
 * 
 * Copyright (c) 2008 Harunobu Oyama <nobukichi at gmail.com>
 * Licensed under GNU GPL version 3
 *
 */

#ifndef		NOBITTER_MODEL_INCLUDED
#define		NOBITTER_MODEL_INCLUDED

#include	<set>
#include	<QStandardItemModel>
#include	<QDateTime>
#include	<QTimer>
#include	"tweet.h"

class QTextStream;
class Filter;

class NobitterModel : public QStandardItemModel
{
	Q_OBJECT

public:
	explicit NobitterModel(QObject * parent = 0);

	//! clear all of the tweet stored in the model
	void init();

	//! get the list of user names in the model
	std::set<QString> getUserNames();

	//! return the date and time of the most recent post in the model
	//! without the own post in the local cache
	QDateTime mostRecentDateTime();

	//! feed a vector of Tweet to the model
	void feedTweets(const TweetVector& tweets);

	//! feed my tweet, which is sent to server, but not received from server.
	//! this should be replaced with the one received from the server afterwards.
	void feedMyTweet(const Tweet& myTweet);

	//! NobitterModel will not delete the filter given to these functions.
	//! It is caller's responsibility to delete it when shutting down.
	void setWhoFilter(Filter* filter);
	void setAtFilter(Filter* filter);
	void setWhenFilter(Filter* filter);

	//! returns the oldest tweet newer than baseTweet.
	//! returns Tweet with empty userName when no such tweet exists.
	Tweet tweetNewerThan(const Tweet& baseTweet);

	//! save the model to the given text stream
	//! return true if succeeded
	bool save(QTextStream& out);

	//! save log of the specified month to the given stream
	void saveLogInMonth(int year, int month, QTextStream& out);

	//! remove tweets made before the given dateBefore
	void removeTweetsBefore(const QDate& dateBefore);

	//! load the model from the given text stream
	//! return true if succeeded
	bool load(QTextStream& in);

signals:
	void signalModelUpdated();

private slots:
	void slotUpdateModel();
	void slotStartUpdateModelTimer();

private:
	//! set of Tweet, that have been sent to server, but not received from server.
	TweetSet myLocalTweet;

	//! set of all Tweet
	TweetSet tweetsArchive;

	Filter* pWhoFilter; //! who filter 
	Filter* pAtFilter; //! @ filter 
	Filter* pWhenFilter; //! when filter

	//! timer to update model
	//! we don't want to update the model every time feeding user tweet as it makes vier flicker
	QTimer updateModelTimer; 

	//! disallow copy and assignment
	NobitterModel(const NobitterModel&);
	NobitterModel& operator=(const NobitterModel&);
};


#endif	//	NOBITTER_MODEL_INCLUDED

